﻿Shader "Outline/OutlintEffect"
{
	Properties
	{
		//自动传入纹理
		_MainTex("Texture", 2D) = "white" {}
		//扣好的边缘纹理
		_OutLineTex("OutLineTex",2D) = "white"{}
		//CommandBuffer渲染的纹理
		_RenderTex("RenderTex",2D) = "white"{}
		//描边的颜色
		_OutlineColor("OutlineColor",Color)=(0,0,0,1)
	}
	//用作高斯模糊的处理程序    
	CGINCLUDE
	sampler2D _MainTex;
	float4 _MainTex_TexelSize;

	struct a2v {
		float4 vertex:POSITION;
		float2 uv:TEXCOORD0;
	};
	struct v2f {
		float4 pos:SV_POSITION;
		float2 uv[5]:TEXCOORD0;
	};

	fixed4 frag(v2f i) :SV_TARGET{
		float3 col = tex2D(_MainTex,i.uv[0]).xyz * 0.4026;
		float3 col1 = tex2D(_MainTex,i.uv[1]).xyz * 0.2442;
		float3 col2 = tex2D(_MainTex,i.uv[2]).xyz * 0.2442;
		float3 col3 = tex2D(_MainTex,i.uv[3]).xyz * 0.0545;
		float3 col4 = tex2D(_MainTex,i.uv[4]).xyz * 0.0545;
		float3 finalCol = col + col1 + col2 + col3 + col4;
		return fixed4(finalCol,1.0);
	}
	ENDCG

	SubShader {
		Cull Off ZWrite Off ZTest Always
		//pass 0 横向模糊
		pass {
			CGPROGRAM
			#include"UnityCG.cginc"
			#pragma vertex vert_heng
			#pragma fragment frag
			//横向扩展
			v2f vert_heng(a2v v) {
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				float2 uv = v.uv;
				o.uv[0] = uv;
				o.uv[1] = uv + float2(1, 0) * _MainTex_TexelSize.xy;
				o.uv[2] = uv + float2(-1, 0) * _MainTex_TexelSize.xy;
				o.uv[3] = uv + float2(2, 0) * _MainTex_TexelSize.xy;
				o.uv[4] = uv + float2(-2, 0) * _MainTex_TexelSize.xy;
				return o;
			}
			ENDCG
		}
		//pass 1 竖直模糊
		pass {
			CGPROGRAM
			#include"UnityCG.cginc"
			#pragma vertex vert_shu
			#pragma fragment frag
			v2f vert_shu(a2v v) {//竖直扩展
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				float2 uv = v.uv;
				o.uv[0] = uv;
				o.uv[1] = uv + float2(0, 1) * _MainTex_TexelSize.xy;
				o.uv[2] = uv + float2(0, -1) * _MainTex_TexelSize.xy;
				o.uv[3] = uv + float2(0, 2) * _MainTex_TexelSize.xy;
				o.uv[4] = uv + float2(0, -2) * _MainTex_TexelSize.xy;
				return o;
			}
			ENDCG
		}
		//pass 2 将没有模糊的图像和模糊后的图像相减获得轮廓图像
		Pass{
			CGPROGRAM
			#include "UnityCG.cginc"
			#pragma vertex vert
			#pragma fragment frag
			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f1
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			v2f1 vert(appdata v)
			{
				v2f1 o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}
			sampler2D _RenderTex;

			fixed4 frag(v2f1 i) : SV_Target
			{

				float3 col = tex2D(_MainTex,i.uv).xyz;
				float3 commandCol = tex2D(_RenderTex,i.uv).xyz;
				float3 finalCol = col - commandCol;
				return fixed4(finalCol,1.0);
			}
			ENDCG
		}
		//pass 3 将正常图像和轮廓图像混合到一起
		Pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct appdata{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f2{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			v2f2 vert(appdata v){
				v2f2 o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}
			sampler2D _OutLineTex;
			fixed4 _OutlineColor;				
			fixed4 frag(v2f2 i) : SV_Target{
				fixed4 col = tex2D(_MainTex, i.uv);
				fixed4 lineCol = tex2D(_OutLineTex,i.uv);
				float a=(lineCol.r+lineCol.g+lineCol.b)/3;
				col.rgb = col.rgb*(1-a)+_OutlineColor*a;
				return col;
			}
			ENDCG
		}
		//pass 4 边缘实体化
		Pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct appdata{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f4{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			v2f4 vert(appdata v){
				v2f4 o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}	
						
			fixed4 frag(v2f4 i) : SV_Target{
				fixed4 whiteCol=(1,1,1,1);
				fixed4 col = tex2D(_MainTex, i.uv);
				if(col.r>0 ||col.g>0 || col.b>0){
					col.rgb = whiteCol;
				}
				return col;
			}
			ENDCG
		}
	}
}
